package com.banshou.installs.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.banshou.installs.R;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.IscreeninfoBinding;
import com.banshou.installs.model.dao.RequestActive;
import com.banshou.installs.model.dao.ResponseActive;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.view.WheelView;
import com.banshou.installs.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cjq on 2017/12/28.
 * Email: stoic_yb@139.com
 * features: 设置屏幕信息
 */

public class ScreenInfoActivity extends BaseActivity implements View.OnClickListener {
    private IscreeninfoBinding mBinding;
    private boolean isStartLc;
    private List<String> strList;
    private String xqId;
    private String zId;
    private String dyId;
    private String dyname;
    private String xqname;
    private String zname;
    private String startLc;
    private String endLc;
    private String qrResult;
    private String xqtype;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.iscreeninfo);
        initView();
    }

    private void initView() {
        strList = new ArrayList<>();
        qrResult = getIntent().getStringExtra(Config.TRANSDATA);
        mBinding.mac.setText(qrResult == null ? "" : qrResult);
        mBinding.title.pagerTitle.setText("激活屏幕");
        mBinding.cancel5.setOnClickListener(this);
        mBinding.cancel8.setOnClickListener(this);
        mBinding.cancel10.setOnClickListener(this);
        mBinding.cancel12.setOnClickListener(this);
        mBinding.cancel16.setOnClickListener(this);
        mBinding.buNext.setOnClickListener(this);
        mBinding.title.back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel5:
                Intent intent = new Intent(this, XQActivity.class);
                startActivityForResult(intent, 101);
                break;
            case R.id.back:
                finish();
                break;
            case R.id.bu_next:
                if (qrResult != null) {
                    activeEle();
                }
                break;
            case R.id.cancel8:
                if (xqId == null) {
                    ToastUtil.toast("请先选择小区");
                    return;
                }
                Intent intent2 = new Intent(this, ZActivity.class);
                intent2.putExtra("XQID", xqId);
                startActivityForResult(intent2, 102);
                break;
            case R.id.cancel10:
                if (zId == null) {
                    ToastUtil.toast("请先选择幢");
                    return;
                }
                Intent intent3 = new Intent(this, DYActivity.class);
                intent3.putExtra("ZID", zId);
                startActivityForResult(intent3, 103);
                break;
            case R.id.cancel12:
                if (dyId == null) {
                    ToastUtil.toast("请先选择单元");
                    return;
                }
                isStartLc = true;
                takeDialog();
                break;
            case R.id.cancel16:
                if (dyId == null) {
                    ToastUtil.toast("请先选择单元");
                    return;
                }
                isStartLc = false;
                takeDialog();
                break;
        }
    }

    /**
     * 激活屏幕
     */
    private void activeEle() {
        RequestActive active;
        if(xqtype.equals("3")){
            active = new RequestActive(xqId, zId, dyId, qrResult, Integer.valueOf(mBinding.tvStartLc.getText().toString()), Integer.valueOf(mBinding.tvEndLc.getText().toString()),mBinding.etPerson1.getText().toString(),mBinding.etPerson2.getText().toString(),mBinding.etPerson3.getText().toString());
        }else{
            active = new RequestActive(xqId, zId, dyId, qrResult, Integer.valueOf(mBinding.tvStartLc.getText().toString()), Integer.valueOf(mBinding.tvEndLc.getText().toString()));
        }
        RetrofitManager2.getInstance()
                .create(ResponseInfo.class)
                .actEle(active)
                .compose(RxUtil.lifecycle(this))
                .subscribe(new Observer<ResponseActive>() {
                    @Override
                    protected void onNextStep(ResponseActive responseActive) {
                        Intent intent1 = new Intent(ScreenInfoActivity.this, FloorSetActivity.class);
                        intent1.putExtra(Config.STARTLC, mBinding.tvStartLc.getText().toString());
                        intent1.putExtra(Config.ENDLC, mBinding.tvEndLc.getText().toString());
                        intent1.putExtra(Config.ELEID, responseActive.getElevator());
                        startActivity(intent1);
                    }

                    @Override
                    protected void onErrorResult() {

                    }
                });
    }

    /**
     * 地址选择
     */
    private void takeDialog() {
        strList.clear();
        View wheelView = LayoutInflater.from(this).inflate(R.layout.dialogwheelview, null);
        WheelView view = wheelView.findViewById(R.id.wheelview);
        TextView title = wheelView.findViewById(R.id.tv_title);
        if (isStartLc) {
            for (int j = -5; j < 2; j++) {
                strList.add(String.valueOf(j));
            }
        } else {
            for (int j = 2; j < 50; j++) {
                strList.add(String.valueOf(j));
            }
        }
        title.setText(isStartLc ? "选择起始楼层" : "选择终止楼层");
        view.setOffset(2);
        view.setItems(strList);
        view.setSeletion(0);
        view.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                if (isStartLc) {
                    startLc = item;
                    checkButton();
                    mBinding.tvStartLc.setText(item);
                    mBinding.tvStartLc.setTextColor(getResources().getColor(R.color.colorBlack));
                } else {
                    endLc = item;
                    checkButton();
                    mBinding.tvEndLc.setText(item);
                    mBinding.tvEndLc.setTextColor(getResources().getColor(R.color.colorBlack));
                }
            }
        });
        final Dialog dialog = new Dialog(this, R.style.edit_AlertDialog_style);
        dialog.setContentView(wheelView);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        TextView button = wheelView.findViewById(R.id.confirm);
        button.setOnClickListener(view1 -> dialog.cancel());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        switch (requestCode) {
            case 101:
                xqId = data.getStringExtra("XQID");
                xqname = data.getStringExtra("XQNAME");
                xqtype = data.getStringExtra("XQTYPE");
                mBinding.srcComm.setText(xqname);
                mBinding.srcComm.setTextColor(getResources().getColor(R.color.colorBlack));
                if (xqtype != null && xqtype.equals("3")) {
                    mBinding.cancelrl.setVisibility(View.VISIBLE);
                } else {
                    mBinding.cancelrl.setVisibility(View.GONE);
                }
                checkButton();
                break;
            case 102:
                zId = data.getStringExtra("ZID");
                zname = data.getStringExtra("ZNAME");
                mBinding.tvLd.setText(zname);
                mBinding.tvLd.setTextColor(getResources().getColor(R.color.colorBlack));
                checkButton();
                break;
            case 103:
                dyId = data.getStringExtra("DYID");
                dyname = data.getStringExtra("DYNAME");
                mBinding.tvDy.setText(dyname);
                mBinding.tvDy.setTextColor(getResources().getColor(R.color.colorBlack));
                checkButton();
                break;
        }
    }

    public void checkButton() {
        if (xqname != null && zname != null && dyname != null && startLc != null && endLc != null) {
            if (!xqtype.equals("3")) {
                mBinding.buNext.setEnabled(true);
            } else {
                if(TextUtils.isEmpty(mBinding.etPerson1.getText()) && TextUtils.isEmpty(mBinding.etPerson2.getText()) && TextUtils.isEmpty(mBinding.etPerson3.getText())){
                    mBinding.buNext.setEnabled(true);
                }else{
                    mBinding.buNext.setEnabled(false);
                }
            }
        } else {
            mBinding.buNext.setEnabled(false);
        }
    }
}
